package com.xian.zk.core;

import com.xian.zk.common.properties.ZookeeperProperties;
import com.xian.zk.model.Instance;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: zoo-task
 * @description:
 * @author: liru.xian
 * @create: 2022-01-17 10:32
 **/
@Slf4j
@Service
public class ServerDataServiceImpl implements ServerDataService {


    private final CuratorFramework client;
    private final ZookeeperProperties zookeeperProperties;

    public ServerDataServiceImpl(CuratorFramework client, ZookeeperProperties zookeeperProperties) {
        this.client = client;
        this.zookeeperProperties = zookeeperProperties;
    }

    /**
     * 获取map 节点
     *
     * @param path 路径节点
     * @return
     */
    @Override
    public Map<String, Instance> getPathNode(String path) {
        return null;
    }

    /**
     * Instance 集合
     *
     * @return
     */
    @Override
    public List<Instance> getAllList() {
        return null;
    }

    /**
     * 获取元数据
     *
     * @param instanceId 实例唯一ID
     * @return
     */
    @Override
    public Map<String, String> getMetadata(String instanceId) {
        Instance instance = DataServerUtil.getInstancesByInstanceId(instanceId);
        return instance.getMetadata();
    }

    /**
     * 获取所有的 serializeId
     *
     * @return
     */
    @Override
    public Set<String> getSerializeId() {
        String parentPath = zookeeperProperties.getParentPath();
        try {
            List<String> paths = client.getChildren().forPath(parentPath);
            return new HashSet<>(paths);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashSet<>();
    }

    /**
     * 获取父类path
     *
     * @return
     */
    @Override
    public String getParentPath() {
        return zookeeperProperties.getParentPath();
    }
}
